﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using SimpleTcpServer.WebSocketProtocols;

namespace SimpleTcpServer
{
    /// <summary>
    /// Use header class to select protocol to return.
    /// </summary>
    public class WebSocketProtocolFactory
    {
        public IWebSocketProtocol ProcessConnectRequest(Connection connection)
        {
            string data = Encoding.UTF8.GetString(connection.Buffer, 0, connection.ReceivedByteCount);
            ////Check first to see if this is a flash socket XML request.
            //if (data == "<policy-file-request/>\0")
            //{
            //    Debug.WriteLine(string.Format("Process flash socket headers"));
                //if it is, we access the Access Policy Server instance to send the appropriate response.
                //context.Server.AccessPolicyServer.SendResponse(context.TcpClient);
                //connection.Disconnect();
            //}
            //else //If it isn't, process http/websocket header as normal.
            //{
                Debug.WriteLine(string.Format("Process http/websocket headers"));
                connection.Header = new Header(data);
                switch (connection.Header.Protocol)
                {
                    case Protocol.WebSocketHybi00:
                        Debug.WriteLine(string.Format("Create WebSocketHybi00 from factory"));
                        return new WebSocketProtocols.hybi00.WebSocketHyBi00Protocol(new WebSocketProtocols.hybi00.Authentication()) { DataFrame = new WebSocketProtocols.hybi00.DataFrame() };
                        //connection.Handler.UnregisterContext(connection);
                        //connection.Handler = WebSocket.hybi00.Handler.Instance;
                        //connection.UserContext.DataFrame = new WebSocket.hybi00.DataFrame();
                        //connection.Handler.RegisterContext(connection);
                        //break;
                    case Protocol.WebSocketRFC6455:
                        Debug.WriteLine(string.Format("Create WebSocketRFC6455 from factory"));
                        return new WebSocketProtocols.rfc6455.WebSocketRfc6455Protocol(new WebSocketProtocols.rfc6455.Authentication()) { DataFrame = new WebSocketProtocols.rfc6455.DataFrame() };
                        //connection.Handler.UnregisterContext(connection);
                        //connection.Handler = WebSocket.rfc6455.Handler.Instance;
                        //connection.UserContext.DataFrame = new WebSocket.rfc6455.DataFrame();
                        //connection.Handler.RegisterContext(connection);
                        //break;
                    //default:
                    //    Debug.WriteLine(string.Format("No protocol selected, return and close connection"));
                        //connection.Header.Protocol = Protocol.None;
                        
                //}
                //if (connection.Header.Protocol != Protocol.None)
                //{
                //    connection.Handler.HandleRequest(connection);
                //}
                //else
                //{
                //    Debug.WriteLine(string.Format("No protocol selected, send not implemened msg."));
                //    connection.UserContext.Send(Response.NotImplemented, true, true);
                //}
            }
            return null;
        }
    }
}
